import { observable, action, toJS, computed } from 'mobx';
import * as DoorPageAPI from '../../apis/task/doorPage';
import * as taskMain from '../../apis/task/taskMain';
import * as taskAdd from '../../apis/task/taskAdd';

export default class TaskDetailStore {
    @observable spinning = false;
    @observable canEdit = false;
    @observable detailKey = -1;
    @observable tabselectKey = 1;
    canShow = false;
    @observable cancreate = false;
    @observable isconfirm = false;
    @observable canaudit = false;
    candel = false;
    canremind = false;
    @observable columnList =[];

    @computed
    get columnFieldRightMap () {
        let m = {}
        this.columnList.forEach((c, i) => {
            m[c.columninfo] = c.fieldright
            if (c.columninfo == "level") {
                m["lev"] = c.fieldright
            }
            if (c.columninfo == "todotype") {
                m["todoval"] = c.fieldright
            }
        })
        return m
    }

    @observable editParams = {
        name: '',//任务名称
        begindate: '',//开始时间
        enddate: '',//结束时间
        remark: '',//描述
        level: '5',//紧急程度
        principalid: '',//责任人
        parentid: '',//上级任务
        partnerid: '',//参与人
        taskids: '',//相关任务
        goalids: '',//相关目标
        docids: '',//相关文档
        wfids: '',//相关流程
        crmids: '',//相关客户
        projectids: '', //相关项目
        fileids: '',//附件
        showallsub: 0,//是否开放下级任务
        sharerid: '',//分享给
        tag: '',//标签
        risk: '',//风险点
        difficulty: '',//困难点
        assist: '',//协助点
        special: '',//是否关注
        status: '',//完成状态
        todotype: '4',//标记todo类型
        creater:'',//创建者
        creatername:'',
        createdate:'',//创建时间
        taskprogress:0,

        parentArray: [],//上级任务
        sublist: [],//下级任务
        principalArray: [],//责任人
        partnerArray: [],//参与人
        taskArray: [],//相关任务
        goalArray: [],//相关目标
        docArray: [],//相关文档
        wfArray: [],//相关流程
        crmArray: [],//相关客户
        projectArray: [],//相关项目
        fileArray: [],//附件
        sharerArray: [],//分享给
        tagArray: [],//标签

    };
    @observable taskLog = [];//操作日志
    lastid = '';
    firstlog =1;//是否第一次请求操作日志
    @observable lastcount = 0;
    @observable fblist = [];//反馈list
    @observable fblastcount = 0;//剩余反馈数量
    feedbackcount = 0;//共有反馈数量
    @observable feedbackContent = '';//反馈内容
    lastfeedbackid = '';
    newfbcount = "";//新反馈信息总数
    @observable optparams = {};//ViewTree组件操作数组
    @observable localtime = new Date().getTime();
    @observable relateInfoDatas = [];//相关信息数组集合
    @observable asslist = [];//被关联任务和被关联目标集合
    @observable addsubname = "";
    errormsg = "";//错误信息
    hasData = false;//是否有数据
    scrollstatus = 0;//滚动条滚动状态
    isFb = undefined;
    checknew = 0;//检查新任务
    iconArray = {
        'goalids':"icon-portal-ModelingEngine","taskids":"icon-coms-currency-Task",
        "docids":"icon-portal-doc","wfids":"icon-coms-Workflow-o","crmids":"icon-portal-crm",
        "projectids":"icon-coms-project","fileids":"icon-coms-link","planids":"icon-coms-release"
    };
    category = "";//附件上传目录
    maxUploadSize = '';//最大上传大小
    @observable cubeLayoutParame = {};//装载布局请求入参
    @observable Card = {};

    @observable dropdisplay = true;
    @observable dropSelectedKey = 1;
    @observable checkedIds = '';
    @observable tagChoiceBoxVisible = false;
    @observable taskitemTaglistPopoverVisible = false;
    @observable subtagvisible = false;
    opentagtype = 1;
    selecttaskid = "";
    @observable allCheckBoxVal = "0";//子任务checkbox框的值
    auditperson = "";
    //获取页面元素值
    @action
    getTaskDetail = (params,_isloading) => {
        if(_isloading){
            this.spinning = true;
        }
        //页面重新渲染时 清空操作日志
        this.taskLog = [];
        this.lastid = '';
        this.firstlog = 1;
        this.lastcount = 0;
        //页面重新渲染时 清空任务反馈
        this.feedbackContent = '';
        this.fblastcount = 0;
        this.feedbackcount = 0;
        this.dropdisplay = true;
        this.dropSelectedKey = 1;
        this.allCheckBoxVal = "0";
        this.fblist = [];
        taskMain.getTaskDetail(params).then(action((data) => {
            if (data.status == 1) {
                this.errormsg ="";
                this.scrollstatus = 1;
                this.checknew = 1;
                this.columnList = data.columnList;
                this.relateInfoDatas = data.relateInfo;
                this.asslist = data.asslist;
                this.editParams.name = data.name;
                this.editParams.begindate = data.begindate;
                this.editParams.enddate = data.enddate;
                this.editParams.remark = data.remark;
                this.editParams.status = data.taskstatus;
                this.editParams.level = data.lev;
                this.editParams.creater = data.creater;
                this.editParams.creatername = data.creatername;
                this.editParams.createdate = data.createdate;
                this.editParams.taskprogress = data.taskprogress;

                this.editParams.principalid = data.principalid || '';
                if(data.principalid){
                    this.editParams.principalArray = [{id:data.principalid ,name:data.principalname}];
                }else{
                    this.editParams.principalArray = [];
                }
                this.editParams.parentid = data.parentid || '';
                if(data.parentid){
                    this.editParams.parentArray = [{id:data.parentid ,name:data.parentname}];
                }else{
                    this.editParams.parentArray = [];
                }
                this.editParams.partnerid = data.partnerids || '';
                this.editParams.partnerArray = data.partnerarry || [];
                this.editParams.docids = data.docids || '';
                this.editParams.docArray = data.docarry || [];
                this.editParams.wfids = data.wfids || '';
                this.editParams.wfArray = data.wfarry || [];
                this.editParams.crmids = data.crmids || '';
                this.editParams.crmArray = data.crmarry || [];
                this.editParams.projectids = data.projectids || '';
                this.editParams.projectarry = data.projectarry || [];
                this.editParams.fileids = data.fileids || '';
                this.editParams.fileArray = data.filearry || [];
                this.editParams.tag = data.tag || '';
                this.editParams.tagArray = data.tagarry || [];
                this.editParams.sharerid = data.sharerids || '';
                this.editParams.sharerArray = data.sharerarry || [];
                this.editParams.goalids = data.goalids || '';
                this.editParams.goalArray = data.goalarry || [];
                this.editParams.taskids = data.taskids || '';
                this.editParams.taskArray = data.rtaskarry || [];

                this.editParams.showallsub = data.showallsub;
                this.editParams.sublist = data.sublist;
                this.editParams.risk = data.risk;
                this.editParams.difficulty = data.difficulty;
                this.editParams.assist = data.assist;
                this.editParams.special = data.special;
                this.editParams.todotype = data.todotype+"" || "4";
                this.fblastcount = data.lastcount || 0;
                this.feedbackcount = data.feedbackcount || 0;
                if(data.newfbcount && data.newfbcount>0){
                    this.newfbcount = {newfbcount:data.newfbcount};
                }else{
                    this.newfbcount = "";
                }
                this.fblist = data.fblist || [];
                if (data.fblist) {
                    this.lastfeedbackid = data.fblist[data.fblist.length - 1].lastid;
                }
                this.auditperson = data.auditperson;
                this.canremind = data.canremind;
                this.isconfirm = data.isconfirm;
                this.canaudit = data.canaudit;
                this.candel = data.candel;
                this.cancreate = data.cancreate;
                this.canEdit = data.canedit;
                this.canShow = data.canedit;
                this.hasData = true;
                //this.getTaskLog({lastid:this.lastid, operation: 'getLog',taskId: params.taskId});
            }else{
                this.hasData = false;
                this.errormsg = data.errormsg;
            }
            if(_isloading){
                this.spinning = false;
            }
        }));
    }
    //建模表单初始化
    @action
    initForm = params => {
        this.tabselectKey = 1;
        taskAdd.getTaskSetupInit(params).then(action((result) => {
            let _isloading =true;
            if(result.status==1 && result.isopen === true){
                this.hasData = true;
                this.cubeLayoutParame = {...result.data};
                _isloading =false;
            }else{
                this.cubeLayoutParame = {};
            }
            this.getTaskDetail(params,_isloading);
        }));
    }
    @action
    getCardStorefun = (store)=>{
        this.spinning = true;
        this.Card = store;
    }
    //编辑保存
    @action
    editTaskInfo = params => {
        this.spinning = true;
        taskMain.editTaskInfo(params).then(action((data) => {
            this.canEdit = true;
            if(params.fieldname=='tag' && data.tagList){
                this.editParams.tagArray = data.tagList;
            }
            if(data.haschange==1 && params.fieldname=='principalid'){
                this.editParams.status = 4;
            }
            if(params.doback){
                params.doback(data.status,data.msg,data.taskTagMap);
            }
            if(params.tagtype=="2" || params.tagtype=="3"){
                this.getSubTaskList({ taskId: this.detailKey,type:'father'});
            }
            if(params.issub!=1){
                if(data.logsMap1){
                    if(this.taskLog.length > 0){
                        this.taskLog.unshift(data.logsMap1);
                    }else{
                        this.taskLog.push(data.logsMap1);
                    }
                }
                if(data.logs){
                    if(this.taskLog.length > 0){
                        this.taskLog.unshift(data.logs);
                    }else{
                        this.taskLog.push(data.logs);
                    }
                }
            }
            this.spinning = false;
        }));
    };

    //获取操作日志
    @action
    getTaskLog = params => {
        this.spinning = true;
        params.lastcount = this.lastcount;
        taskMain.getTaskLog(params).then(action((data) => {
            if (data.status == 1) {
                this.lastcount = data.lastcount;
                this.lastid = data.lastid;
                if (params.type == 'more') {
                    this.taskLog = this.taskLog.concat(data.datas);
                } else {
                    this.taskLog = data.datas;
                }
            }
            this.spinning = false;
        }))
    }

    //删除反馈 //获取反馈
    @action
    taskOperation = params => {
        this.spinning = true;
        taskMain.taskOperation(params).then(action((data) => {
            if (data.status == 1) {
                if (params.operation == 'delFbInfo') {
                    let index = params.index;
                    this.feedbackcount = this.feedbackcount - 1;
                    this.fblist.remove(this.fblist[index]);
                    if (params.doback) {
                        params.doback();
                    }
                } else if (params.operation == 'getFbInfo') {
                    this.fblist = this.fblist.concat(data.datas);
                    this.fblastcount = data.lastcount;
                    this.lastfeedbackid = data.datas[data.datas.length - 1].lastid;
                }
            }
            this.spinning = false;
        }))
    }

    // 添加反馈 
    @action
    addTaskFbInfo = params => {
        this.spinning = true;
        taskMain.addTaskFbInfo(params).then(action((data) => {
            if (data.status == 1) {
                this.feedbackContent = '';
                this.feedbackcount = this.feedbackcount + 1;
                this.fblist.unshift(data.datas[0]);
                if(data.logs){
                    if(this.taskLog.length > 0){
                        this.taskLog.unshift(data.logs);
                    }else{
                        this.taskLog.push(data.logs);
                    }
                }
                if (params.doback) {
                    params.doback();
                }
            }
            this.spinning = false;
        }));
    }

    //添加关注或取消关注
    @action
    setSpecial = params => {
        this.spinning = true;
        params.status = this.editParams.status; //任务状态
        params.canedit = this.canEdit?"1":"0";
        params.candel = this.candel?"1":"0";
        taskMain.setSpecial(params).then(action((data) => {
            if (data.status == 1) {
                if (this.taskLog.length > 0) {
                    this.taskLog.unshift(data.logs);
                }
                if (this.editParams.special == 0) {
                    this.editParams.special = 1;
                } else {
                    this.editParams.special = 0;
                }
                params.doback(data.sList);
            }
            this.spinning = false;
        }));
    }

    //修改任务状态
    @action
    toChangeStatus = params => {
        this.spinning = true;
        params.special = this.editParams.special; //关注状态
        params.canedit = this.canEdit?"1":"0";
        params.candel = this.candel?"1":"0";
        DoorPageAPI.toChangeStatus(params).then(action((data) => {
            if (data.status == 1) {
                if (params.clickType == 'child') {
                    let comsbtn = params.comsbtn;
                    let arr = data.smap;
                    comsbtn.map((element, idx) => {
                        if (element.type == params.type) {
                            comsbtn[idx] = arr;
                        }
                    });
                    this.optparams = {};
                    this.optparams.comsbtn = comsbtn;
                    this.optparams.opttype = "update";
                    this.optparams.optkey = params.taskId;
                    this.optparams.status = data.newstatus || params.status;
                    this.localtime =  new Date().getTime();
                } else {
                    // if(params.status==2){
                    //     if(!(data.newstatus && data.newstatus==6)){
                    //         this.editParams.taskprogress = 100;
                    //     }
                    //     this.canremind = false;
                    // }
                    if(params.status==102){
                        this.editParams.status = 4;
                    }
                    if(params.status==2 ||params.status==100 || params.status==104 || params.status==105){
                        this.initForm({ taskId:this.detailKey});
                    }else{
                        if (this.taskLog.length > 0) {
                            this.taskLog.unshift(data.logs);
                        }
                    }
                }
                params.doback(data.sList,data.candel,data.newstatus);
            }
            this.spinning = false;
        }));
    }

    //任务详情页页子列表查询
    @action
    getSubTaskList = params => {
        this.spinning = true;
        params.showallsub = this.editParams.showallsub;
        taskMain.getSubTaskList(params).then(action((data) => {
            if (data.status == 1) {
                if (params.type == 'father') {
                    this.dropdisplay = true;
                    this.allCheckBoxVal = "0";
                    this.editParams.sublist = data.datas;
                } else {
                    this.optparams = {};
                    this.optparams.datas = data.datas;
                    this.optparams.opttype = "childs";
                    this.optparams.optkey = params.taskId;
                    this.localtime = new Date().getTime();
                }
            }
            this.spinning = false;
        }));
    }
   @action
   addSubTask = params =>{
    taskMain.addTask(params).then(action((data) => {
         if(data.status==1){
            //this.getSubTaskList({ taskId: params.parentid,type:'father'});
            this.optparams = {datas:data.subdatas,optkey:data.taskId,opttype:"add"};
            this.localtime =  new Date().getTime();
            params.doback({datas:data.datas,optkey:data.taskId,optdatetype:data.datetype});
         }
    }));
   }
   @action
   setNewTaskInfo(params,currentAdd){
       let arry = {};
       if(currentAdd==2){
        arry.opttype = "addchild";
        arry.optkey = params.parentid;
       }else{
        arry.opttype = "add";
        arry.optkey = params.optkey;
       }
       arry.datas = params.subdatas;
       this.optparams = arry;
       this.localtime =  new Date().getTime();
   }
   @action
   viewDocFiled = params =>{
    taskMain.viewDocFiled(params).then(action((data) => {
         if(data.status){
            params.doback();
         }
    }));
   }
   @action("点击任务信息事件")
   onNodeClick= params =>{
      this.detailKey = params.key;
      this.initForm({ taskId:params.key});
   }

   @action
   changeTagChoiceBoxVisible = (visible) => {
       this.tagChoiceBoxVisible = visible
   }
   @action
   changeTaskitemTaglistPopoverVisible = (visible) => {
       this.taskitemTaglistPopoverVisible = visible
   }
   @action
   changeSubTagBoxVisible = (visible,_type,key) => {
       this.subtagvisible = visible;
       this.opentagtype = _type;
       this.selecttaskid = key;
   }
   @action("批量删除")
   batchDelTask = params =>{
        this.spinning = true;
        params.operation = "batchDel";
        DoorPageAPI.toChangeStatus(params).then(action((data) => {
            this.getSubTaskList({ taskId: this.detailKey,type:'father'});
            if(params.doback){
                params.doback(data.status);
            }
            this.spinning = false;
        }))
   }
   @action("重置dropdisplay")
   setDropDisplay(isSelect,isallselect){
      if(isSelect){
         this.dropdisplay = false;
         if(isallselect){
            this.allCheckBoxVal = "1";
         }else{
            this.allCheckBoxVal = "0";
         }
      }else{
         this.dropdisplay = true;
         this.allCheckBoxVal = "0";
      }
   }
}